<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<script type="text/javascript" src="scripts/boot.js"></script>
	<style type="text/css">
	    .errorText
	    {
	        color:red;
	    }
    </style>
</head>
<body>
	<form id="form1" method="post">
        <div style="padding-left:40px;padding-bottom:15px;padding-top: 15px;">
       	<input name="id" class="mini-hidden" />
       	<input id="medicine_id" class="mini-hidden" />
       	<input name="storageId" class="mini-hidden" />
            <table style="width:100%;" cellspacing="0" cellpadding="10">
                <tr>
                    <td style="width:80px;">药品名称：</td>
                    <td style="width:150px;">
    					<input id="tbl1" name="medicineId" errorMode="none" onvalidation="checkValidation"  requiredErrorText="请输入药品" onvaluechanged="checkMedicine();" valueFromSelect="true" class="mini-autocomplete" required="true" style="width:180px;" valueField="id" textField="name" url="storage_detail_find_medicine.do" value="cn" emptyText="请输入药品名称"/>
    				</td>
    				<td id="medicineId_error" class="errorText" ></td>
                </tr>
                <tr>
                    <td style="width:80px;">数量：</td>
                    <td>
                    	<input id="sp1" name="quantity" changeOnMousewheel="false" class="mini-spinner" minValue="1" maxValue="999999" style="width:180px;" onvaluechanged="onValueChanged"/>
                    </td>
                </tr>
                <tr>
                	<td style="width:80px;">生产日期：</td>
                    <td>
                    	<input name="productionDate"  errorMode="none" onvalidation="checkValidation" requiredErrorText="请选择日期" class="mini-datepicker" required="true" style="width: 180px" emptyText="请选择日期" allowInput="false"/>
                    </td>
                    <td id="productionDate_error" class="errorText" ></td>
                </tr>
                <tr>
                    <td style="width:80px;">有效期至：</td>
                    <td>
                    	<input name="validTo"  errorMode="none" onvalidation="checkValidation" requiredErrorText="请选择日期" class="mini-datepicker" required="true" style="width: 180px" emptyText="请选择日期" allowInput="false"/>
                    </td>
                    <td id="validTo_error" class="errorText" ></td>
                </tr>
            </table>
        </div>
        <div style="text-align:center;padding-top:80px;">               
            <a class="mini-button" onclick="onOk" style="width:60px;margin-right:50px;">确定</a>       
            <a class="mini-button" onclick="onCancel" style="width:60px;">取消</a>       
        </div> 
    </form>

<script type="text/javascript">
	mini.parse();
	
	//验证药品是否已经添加
	var mId = mini.get("medicine_id");
	var medicine = mini.getbyName("medicineId");
	function checkMedicine(){
		var flag = true;
		if(mId.getValue() != medicine.getValue()){
			$.ajax({
		        url: "storage_detail_check_medicine.do",
				type: 'post',
				data: {storageId: mini.getbyName("storageId").getValue(), medicineId: medicine.getValue()},
				dataType: 'json',
		        cache: false,
		        async: false,
		        success: function (text) {
		            if(text.able){
		            	flag = true;
		            }else{
		            	mini.alert("所选药品已存在列表中！");
		            	flag = false;
		            }
		        },
		        error: function (jqXHR, textStatus, errorThrown) {
		            alert(jqXHR.responseText);
		            window.CloseOwnerWindow();
		        }
		    });
		}
		return flag;
	}
	
	var form = new mini.Form("#form1");
	
	//保存数据
	function SaveData() {
	    var o = form.getData(true); 
	    form.validate();
    	if (form.isValid() == false || checkMedicine() == false) return;
	    $.ajax({
	        url: "storage_detail_edit_save.do",
			type: 'post',
			dataType: 'json',
	        data: o,
	        cache: false,
	        success: function (text) {
	            if(text.success){
	            	window.CloseOwnerWindow("succ");
	            }else{
	            	mini.showTips({content:text.message,state:'danger',x:'center',y:'top'});
	            }
	        },
	        error: function (jqXHR, textStatus, errorThrown) {
	            alert(jqXHR.responseText);
	            window.CloseOwnerWindow();
	        }
	    });
	}
	
	//标准方法接口定义
    function SetData(data) {
        if (data.action == "edit") {
            //跨页面传递的数据对象，克隆后才可以安全使用
            data = mini.clone(data);
            $.ajax({
                url: "storage_detail_find.do?storageDetail.id=" + data.id,
                type: 'post',
                cache: false,
                dataType: "json",
                success: function (text) {
                	form.setData(text.storageDetail);
                	medicine.setText(text.storageDetail.name);
                	mId.setValue(text.storageDetail.medicineId);
                	mini.getbyName("storageId").setValue(text.storageDetail.storageId);
                    form.setChanged(false);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.responseText);
                    CloseWindow();
                }
            });
        }
    }
	
    function GetData() {
        var o = form.getData();
        return o;
    }
    
    function CloseWindow(action) {            
        if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
        else window.close();            
    }
	
    function onOk(e) {
        SaveData();
    }
    
    function onCancel(e) {
        CloseWindow("cancel");
    }
    
    function updateError(e) {
        var id =e.sender.name + "_error";
        var el = document.getElementById(id);
        if (el) {
            el.innerHTML =e.errorText;
        }
    }
    
    function checkValidation(e) {                
        updateError(e);
    }

</script>   
</body>
</html>